package com.controller;

import com.base.AjaxRequest;
import com.base.AjaxResult;
import com.base.BaseController;
import com.base.MetaInfo;
import com.bean.*;
import com.github.pagehelper.PageHelper;
import com.mapper.mysql.WebsiteVisitorMapper;
import com.mapper.repository.mysql.*;
import com.service.WebsiteVisitorService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Log4j2
@RestController
@RequestMapping("visitor")
public class WebsiteVisitorController extends BaseController {

    @Autowired
    private WebsiteVisitorMapper websiteVisitorMapper;

    @Autowired
    private WebsiteVisitorRepository websiteVisitorRepository;

    @Autowired
    private WebsiteVisitorApiRepository websiteVisitorApiRepository;

    @Autowired
    private WebsiteVisitorAbnormalRepository websiteVisitorAbnormalRepository;

    @Autowired
    private WebsiteVisitorUrlRepository websiteVisitorUrlRepository;

    //页面访问记录
    @PostMapping("list")
    public AjaxResult list(@RequestBody AjaxRequest<WebsiteVisitor> request){
        WebsiteVisitor websiteVisitor = request.getT();
        MetaInfo metaInfo = request.getMetaInfo();

        PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize());
        List<WebsiteVisitor> list = websiteVisitorMapper.list(websiteVisitor);

        return success("查询成功",list,metaInfo);
    }

    //接口访问记录
    @PostMapping("listApi")
    public AjaxResult listApi(@RequestBody AjaxRequest<WebsiteVisitorApi> request){
        WebsiteVisitorApi websiteVisitorApi = request.getT();
        MetaInfo metaInfo = request.getMetaInfo();

        PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize());
        List<WebsiteVisitorApi> list = websiteVisitorMapper.listApi(websiteVisitorApi);

        return success("查询成功",list,metaInfo);
    }

    //异常访问记录
    @PostMapping("listAbnormal")
    public AjaxResult listAbnormal(@RequestBody AjaxRequest<WebsiteVisitorAbnormal> request){
        WebsiteVisitorAbnormal websiteVisitorAbnormal = request.getT();
        MetaInfo metaInfo = request.getMetaInfo();

        PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize());
        List<WebsiteVisitorAbnormal> list = websiteVisitorMapper.listAbnormal(websiteVisitorAbnormal);

        return success("查询成功",list,metaInfo);
    }

    //网址访问记录
    @PostMapping("listUrl")
    public AjaxResult listUrl(@RequestBody AjaxRequest<WebsiteVisitorUrl> request){
        WebsiteVisitorUrl websiteVisitorUrl = request.getT();
        MetaInfo metaInfo = request.getMetaInfo();

        PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize());
        List<WebsiteVisitorUrl> list = websiteVisitorMapper.listUrl(websiteVisitorUrl);

        return success("查询成功",list,metaInfo);
    }

    @PostMapping("del")
    public AjaxResult del(@RequestBody WebsiteVisitor websiteVisitor){
        websiteVisitorRepository.deleteById(websiteVisitor.getId());
        return success("删除成功");
    }

    @PostMapping("delAll")
    public AjaxResult delAll(@RequestBody WebsiteVisitor websiteVisitor){
        websiteVisitorMapper.deleteVistorByAddr(websiteVisitor.getAddr());
        return success("删除成功");
    }

    @PostMapping("delApi")
    public AjaxResult delApi(@RequestBody WebsiteVisitorApi websiteVisitorApi){
        websiteVisitorApiRepository.deleteById(websiteVisitorApi.getId());
        return success("删除成功");
    }

    @PostMapping("delAllApi")
    public AjaxResult delAllApi(@RequestBody WebsiteVisitor websiteVisitor){
        websiteVisitorMapper.deleteVisitorApi(websiteVisitor.getAddr());
        return success("删除成功");
    }

    @PostMapping("delAbnormal")
    public AjaxResult delAbnormal(@RequestBody WebsiteVisitorAbnormal websiteVisitorAbnormal){
        websiteVisitorAbnormalRepository.deleteById(websiteVisitorAbnormal.getId());
        return success("删除成功");
    }

    @PostMapping("delUrl")
    public AjaxResult delUrl(@RequestBody WebsiteVisitorUrl websiteVisitorUrl){
        websiteVisitorUrlRepository.deleteById(websiteVisitorUrl.getId());
        return success("删除成功");
    }

    @Autowired
    private WebsiteVisitorDisableRepository websiteVisitorDisableRepository;

    //添加到异常访问
    @PostMapping("addAbnormal")
    public AjaxResult addAbnormal(@RequestBody WebsiteVisitorAbnormal websiteVisitorAbnormal){
        String id = UUID.randomUUID().toString();
        websiteVisitorAbnormal.setId(id);
        websiteVisitorAbnormal.setCreatetime(LocalDateTime.now());

        websiteVisitorAbnormalRepository.save(websiteVisitorAbnormal);
        websiteVisitorMapper.deleteVisitorApi(websiteVisitorAbnormal.getAddr());
        websiteVisitorMapper.deleteVistorByAddr(websiteVisitorAbnormal.getAddr());

        return success("新增成功");
    }

    //添加到禁止访问
    @PostMapping("addDisable")
    public AjaxResult addDisable(@RequestBody WebsiteVisitorDisable websiteVisitorDisable){
        String id = UUID.randomUUID().toString();
        websiteVisitorDisable.setId(id);
        websiteVisitorDisable.setCreatetime(LocalDateTime.now());

        websiteVisitorDisableRepository.save(websiteVisitorDisable);

        websiteVisitorMapper.deleteVistorByAddr(websiteVisitorDisable.getAddr());
        websiteVisitorMapper.deleteVisitorApi(websiteVisitorDisable.getAddr());
        websiteVisitorMapper.deleteAbnormalByAddr(websiteVisitorDisable.getAddr());

        return success("新增成功");
    }

    @Autowired
    private WebsiteVisitorService websiteVisitorService;

    @GetMapping("clean")
    public AjaxResult clean(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String start ="开始执行定时任务"+simpleDateFormat.format(new Date());
        log.info(start);

        websiteVisitorService.handleVisitorApiAbnormal();
        websiteVisitorService.handleVisitorAbnormal();

        String end = "结束执行定时任务"+simpleDateFormat.format(new Date());
        log.info(end);

        return success("成功");
    }


}
